package com.xsomnus.data_structures.searching;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/18 0018 16:19
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class BTree<Key extends Comparable<Key>, Value> {

    private static final int M = 4;
    private Node root;
    private int height;
    private int size;


    public BTree() {
        root = new Node(0);
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public int height() {
        return height;
    }


    private Value search(Node x, Key key, int ht) {
        Entry[] children = x.children;
        if (ht == 0) {
            for (int j = 0; j < x.m; j++) {
                if (eq(key, children[j].key)) {
                    return (Value) children[j].val;
                }
            }
        } else {
            for (int j = 0; j < x.m; j++)
            {
                if (j+1 == x.m || less(key, children[j+1].key))
                {
                    return search(children[j].next, key, ht-1);
                }
            }
        }
        return null;
    }

    private boolean less(Comparable k1, Comparable k2)
    {
        return k1.compareTo(k2) < 0;
    }

    private boolean eq(Comparable k1, Comparable k2) {
        return k1.compareTo(k2) == 0;
    }


    private static final class Node {
        // 子节点的个数
        private int m;
        private Entry[] children = new Entry[m];

        private Node(int k) {
            m = k;
        }
    }

    private static class Entry {
        private Comparable key;
        private Object val;
        private Node next;

        private Entry(Comparable key, Object val, Node next) {
            this.key = key;
            this.val = val;
            this.next = next;
        }
    }


}
